package com.wnxy.pawnbackgroundsystem.controller;

import com.wnxy.pawnbackgroundsystem.commons.OssTemplate;
import com.wnxy.pawnbackgroundsystem.response.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.time.LocalDateTime;

@RestController
@RequestMapping("/file")
public class FileController {
    @Autowired
    private OssTemplate ossTemplate;

    //上传文件
    @PostMapping("/upload")
    @CrossOrigin("*")
    public Result uploadFile(MultipartFile file)throws IOException {
        System.out.println(1111);
        String name = file.getOriginalFilename();
        name=System.currentTimeMillis()+name.substring(name.lastIndexOf("."));
        name= LocalDateTime.now().getYear()+"/"+LocalDateTime.now().getMonth()+"/"+name;
        String url=ossTemplate.uploadFile("groupsix",name,file.getInputStream());
        return new Result(true,"2002","上传成功",url);
    }
}
